﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaDataModel;

namespace MediaClient.Shelf
{
    public partial class ModifyShelfForm : BaseSmallForm
    {
        private CCTV6Entities db;
        private string shelfSeriesNum = null;
        private OfflineShelf currentShelf = null;

        public ModifyShelfForm()
        {
            InitializeComponent();
        }

        public ModifyShelfForm(string shelfSeriesNum):base()
        {
            lblLogo.Image = global::MediaClient.Properties.Resources.修改货架;
            this.shelfSeriesNum = shelfSeriesNum;
        }
        
        
        #region Events

        private void lblControl_Click(object sender, EventArgs e)
        {
            Label currentLabel = sender as Label;
            if (currentLabel != null)
            {
                switch (currentLabel.Name)
                {
                    case "lblClose":
                        this.Close();
                        break;
                    case "lblSubmit":
                        if (currentShelf == null)
                            AddShelf();
                        else
                            ModifyShelf();
                        break;
                }
            }
        }

        private void lblControl_MouseMove(object sender, MouseEventArgs e)
        {
            Label currentLabel = sender as Label;
            switch (currentLabel.Name)
            {
                case "lblClose":
                    lblClose.Image = global::MediaClient.Properties.Resources.窗口关闭按钮_1;
                    break;
                case "lblSubmit":
                    lblSubmit.Image = global::MediaClient.Properties.Resources.确定_1;
                    break;
            }
        }

        private void lblControl_MouseLeave(object sender, EventArgs e)
        {
            Label currentLabel = sender as Label;
            switch (currentLabel.Name)
            {
                case "lblClose":
                    lblClose.Image = global::MediaClient.Properties.Resources.窗口关闭按钮;
                    break;
                case "lblSubmit":
                    lblSubmit.Image = global::MediaClient.Properties.Resources.确定;
                    break;
            }
        }

        private void ModifyShelfForm_Load(object sender, EventArgs e)
        {
            db = new CCTV6Entities();
            InitializeControls();
        }

        private void rbShelfDisplayStyle_Click(object sender, EventArgs e)
        {
            RadioButton currentRadio = sender as RadioButton;
            if (currentRadio != null)
            {
                if (currentRadio.Name == "rbHorizontal")
                {
                    rbVertical.Checked = false;
                    rbHorizontal.Checked = true;
                }
                else
                {
                    rbVertical.Checked = true;
                    rbHorizontal.Checked = false;
                }
            }
        }

        private void txtRumTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox oObject = sender as TextBox;
            if (oObject.Text == "")
                return;
            try
            {
                short.Parse(oObject.Text.Trim());
            }
            catch (FormatException myE)
            {
                MessageBox.Show("请输入数字。", BaseForm.MessageBoxCaption);
            }
            catch (OverflowException myE)
            {
                MessageBox.Show("输入的数字有点大。", BaseForm.MessageBoxCaption);
            }
        }

        
        #endregion

        #region private methods

        private void AddShelf()
        {
            if( CheckShelfContent(true) == false)
                return;
            
            currentShelf = new OfflineShelf();
            currentShelf.ShelfName = txtName.Text.Trim();
            currentShelf.SeriesNumber = txtSeriesNum.Text.Trim();
            currentShelf.RowNumber = short.Parse(txtRowNum.Text.Trim());
            currentShelf.ColumnNumber = short.Parse(txtColumnNum.Text.Trim());
            currentShelf.ColumnCapacity = 2;
            if (cbPrimary.Checked == false && cbSecondary.Checked == false)
            {
                MessageBox.Show("请选择离线柜类型",BaseForm.MessageBoxCaption);
                return;
            }
            if (cbPrimary.Checked == true)
            {
                if (cbSecondary.Checked == false)
                    currentShelf.ShelfDiscType = 1;
                else
                    currentShelf.ShelfDiscType = 3;
            }
            else
                currentShelf.ShelfDiscType = 2;
            try
            {
                db.OfflineShelves.AddObject(currentShelf);
                db.SaveChanges();
                MessageBox.Show("离线柜创建成功！", BaseForm.MessageBoxCaption);
                txtName.Text = txtSeriesNum.Text = "";
            }
            catch (Exception myE)
            {
                throw myE;
            }
        }

        private void ModifyShelf()
        {
            if (CheckShelfContent(false) == false)
                return;
            currentShelf.ShelfName = txtName.Text.Trim();
            currentShelf.SeriesNumber = txtSeriesNum.Text.Trim();
            currentShelf.RowNumber = short.Parse(txtRowNum.Text.Trim());
            currentShelf.ColumnNumber = short.Parse(txtColumnNum.Text.Trim());
            if (cbPrimary.Checked == false && cbSecondary.Checked == false)
            {
                MessageBox.Show("请选择离线柜类型", BaseForm.MessageBoxCaption);
                return;
            }
            if (cbPrimary.Checked == true)
            {
                if (cbSecondary.Checked == false)
                    currentShelf.ShelfDiscType = 1;
                else
                    currentShelf.ShelfDiscType = 3;
            }
            else
                currentShelf.ShelfDiscType = 2;
            try
            {
                db.SaveChanges();
                MessageBox.Show("离线柜修改成功！", BaseForm.MessageBoxCaption);
                this.Close();
            }
            catch (Exception myE)
            {
                throw myE;
            }
        }

        private void InitializeControls()
        {
            if (string.IsNullOrEmpty(this.shelfSeriesNum) == false)
            {
                currentShelf = db.OfflineShelves.Where(shelf => shelf.SeriesNumber == this.shelfSeriesNum).SingleOrDefault();
                if (currentShelf != null)
                {
                    txtSeriesNum.Text = currentShelf.SeriesNumber;
                    txtName.Text = currentShelf.ShelfName;
                    txtRowNum.Text = currentShelf.RowNumber.ToString();
                    txtColumnNum.Text = currentShelf.ColumnNumber.ToString();
                    if (currentShelf.DisplayStyle == true)
                        rbHorizontal.Checked = true;
                    else
                        rbVertical.Checked = true;
                    switch (currentShelf.ShelfDiscType)
                    {
                        case 2:
                            cbSecondary.Checked = true;
                            break;
                        case 3:
                            cbPrimary.Checked = true;
                            cbSecondary.Checked = true;
                            break;
                        case 1:
                        default:
                            cbPrimary.Checked = true;
                            break;
                    }

                    if (currentShelf != null && currentShelf.DiscShelves.Count > 0)
                    {
                        txtColumnNum.ReadOnly = true;
                        txtRowNum.ReadOnly = true;
                        cbPrimary.Enabled = false;
                        cbSecondary.Enabled = false;
                    }
                }
            }
        }

        private bool CheckShelfContent(bool addShelf)
        {
            if (txtSeriesNum.Text.Trim() != "")
            {
                if (addShelf == true)
                {
                    if (db.OfflineShelves.Where(shelf => shelf.SeriesNumber == txtSeriesNum.Text.Trim()).SingleOrDefault() != null)
                    {
                        MessageBox.Show("输入的离线柜标识已存在！", BaseForm.MessageBoxCaption);
                        return false;
                    }
                }
                else
                {
                    if (currentShelf != null)
                    {
                        if(currentShelf.SeriesNumber != txtSeriesNum.Text.Trim())
                        {
                            if (db.OfflineShelves.Where(shelf => shelf.SeriesNumber == txtSeriesNum.Text.Trim()).SingleOrDefault() != null)
                            {
                                MessageBox.Show("输入的离线柜标识已存在！", BaseForm.MessageBoxCaption);
                                return false;
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入离线柜标识。", BaseForm.MessageBoxCaption);
                txtSeriesNum.Focus();
                return false;
            }
            
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("请输入离线柜名称。", BaseForm.MessageBoxCaption);
                txtName.Focus();
                return false;
            }
            if (txtRowNum.Text.Trim() == "")
            {
                MessageBox.Show("请输入离线柜的行数。", BaseForm.MessageBoxCaption);
                txtRowNum.Focus();
                return false;
            }

            if (txtColumnNum.Text.Trim() == "")
            {
                MessageBox.Show("请输入离线柜每行的列数。", BaseForm.MessageBoxCaption);
                txtColumnNum.Focus();
                return false;
            }
            return true;
        }

        #endregion


    }
}
